﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <a href="http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/">Prototypès</a>
</head>
<body>
    <p>First, there is a <b>prototype property</b> that every JavaScript function has (it is empty by default), and you attach properties and methods on this prototype property when you want to implement inheritance</p>
    <p>The second concept with prototype in JavaScript is the <b>prototype attribute</b>. Think of the prototype attribute as a characteristic of the object, and it specifies who the object’s “parent” is</p>
    <div>
        <strong>Constructor</strong><br />
        Before we continue, let’s briefly examine the constructor. A <strong>constructor</strong> is a function used for initializing new objects, and you use the new keyword to call the constructor.
        <p>constructor property is simply a property (like any variable) that holds or points to the constructor of the object.</p>
    </div>
    <p>All objects created with object literals and with the Object constructor inherits from Object.prototype</p>
    <p>Objects created with the new keyword and any constructor other than the Object () constructor, get their prototype from the constructor function.</p>
    <div>
        <strong>This in essence is the prototype chain:</strong> the chain from an object’s prototype to its prototype’s prototype and onwards. And JavaScript uses this prototype chain to look for properties and methods of an object.
    </div>
    <div>
        <strong>Object.prototype Properties Inherited by all Objects</strong><br>
        All objects in JavaScript inherit properties and methods from Object.prototype. These inherited properties and methods are <em>constructor, hasOwnProperty (), isPrototypeOf (), propertyIsEnumerable (), toLocaleString (), toString (), and valueOf ()</em>. ECMAScript 5 also adds 4 accessor methods to Object.prototype
    </div>
</body>
</html>
<script type="text/javascript">
    function PrintStuff(myDocuments) {
        this.documents = myDocuments;
    }

    // We add the print () method to PrintStuff prototype property so that other instances (objects) can inherit it:
    PrintStuff.prototype.print = function () {
        console.log(this.documents);
    };

    // inheritance
    function Plant() {
        this.country = "Mexico";
        this.isOrganic = true;
    }
    function Fruit(fruitName, fruitColor) {
        this.name = fruitName;
        this.color = fruitColor;
    }
    // Set the Fruit's prototype to Plant's constructor, thus inheriting all of Plant.prototype methods and properties.
    Fruit.prototype = new Plant();
</script>
